package com.example.demo.producer;

import com.example.utils.UserContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;

/**
 * @Author xiao
 * @Date 2025/5/6 上午 11:26
 * @ClassName: NumberDemo
 * @Desc:
 */
@Service
public class NumberDemo {
    public  void calcNum( Integer num ) {
        CompletableFuture.runAsync (() ->{
            //System.out.println("输入查找水仙花的最大数字");
            Scanner sc = new Scanner(System.in);

                //int num = sc.nextInt();
                UserContext.setUserId(1000L);
                List<Integer> list = new ArrayList<>();
                for (int i = 100; i < num; i++) {
                    // 计算位数
                    int pow = (int)(Math.log10(Math.abs(i)) + 1);
                    if(i == calculationSum(i,pow) ){
                        list.add(i);
                    }
                }
                System.out.printf("找到水仙花%d个%n",list.size());
                System.out.println(list);
                int a = 1/0;
        }).exceptionally(ex -> {
            ex.getMessage();
            System.out.print("用户id:"+ UserContext.getUserId());
            return null ;
        });
    }

    //各n次方之和
    public static Integer calculationSum(int num,int pow){
        // 循环结束条件
        if ( num == 0 ){
            return 0;
        }
        int digit = num % 10; // 取最后一位
        return (int)Math.pow(digit,pow) +  + calculationSum(num / 10, pow);
    }
}
